<template>
  <div class="lsc-welcome">
    <img src="../assets/page_bg.jpg" width="100%">
  </div>
</template>

<script>
// oIfVo1AQmaozBsUUuWhyR4ip_Slc
import api from '../api';

export default {
  name: 'welcome',
  created() {

    //域名注册
    let signLink = location.href.split('#')[0]

    // 是否存在openid
    if(!localStorage.openId) {
      // 获取openid
      this.getOpenid(signLink)
    }else {
      // 验证注册
      this.getAuthenticate(signLink)
    }

  },
  methods: {
    getOpenid(signLink) {

      // 获取openid
      let _this = this;
      var src = window.location.search;
      var a = src.indexOf("code");
      var b = src.indexOf("&state");
      var code = src.substr(a + 5, b - (a + 5));

      this.axios({
        method: "get",
        url: api.weiXin.UserInfo,
        params: {
          code: code,
          state: "STATE"
        },
        responseType: "text"
      })
        .then(rep => {
          localStorage.openId = rep.data;
          this.getAuthenticate(signLink);
        })
        .catch(err => {
          alert("OPENID API ERROR!");
        });
    },
    getAuthenticate(signLink) {
      if(signLink.indexOf('find.html')>0) {
        // 找服务
        this.$router.push('/find')

      }else if(signLink.indexOf('demand.html')>0) {
        // 我的需求
        this.$router.push('/demandlist')

      }else {
        this.axios.get(api.authenticate.userStatus)
          .then(res => {
          
            //公众号入口
            this.jumpToPage(signLink)
          });
      }
    },
    jumpToPage(signLink) {
      // 找工作
      if(signLink.indexOf('work.html')>0) {
        this.$router.push('/trainlist')
      }
      // 报名培训
      if(signLink.indexOf('index.html')>0) {
        this.$router.push('/trainlist')
      }
      // 我的培训
      if(signLink.indexOf('train.html')>0) {
        this.$router.push('/train')
      }
      // 个人信息
      if(signLink.indexOf('info.html')>0) {
        this.$router.push('/info')
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
